package models

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/cache"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

type FriendGroupExtendModel interface {
	Count(ctx context.Context, userId int64) (int64, error)
	FindList(ctx context.Context, userId, offset, limit int64) ([]*FriendGroup, error)
}

func NewFriendGroupExtendModel(conn sqlx.SqlConn, c cache.CacheConf) FriendGroupExtendModel {
	return &defaultFriendGroupModel{
		CachedConn: sqlc.NewConn(conn, c),
		table:      "friend_group",
	}
}

func (m *defaultFriendGroupModel) Count(ctx context.Context, userId int64) (int64, error) {
	var count int64
	query := fmt.Sprintf("select count(*) from %s where `user_id` = ? and `status` = 0", m.table)
	err := m.QueryRowsNoCacheCtx(ctx, &count, query, userId)
	return count, err
}

func (m *defaultFriendGroupModel) FindList(ctx context.Context, userId, offset, limit int64) ([]*FriendGroup, error) {
	var list []*FriendGroup
	query := fmt.Sprintf("select * from %s where `user_id` = ? and `status` = 0 order by `sort` desc limit ?, ?", m.table)
	err := m.QueryRowsNoCacheCtx(ctx, &list, query, userId, offset, limit)
	return list, err
}
